package uk.ac.lkl.common.ui;



import java.awt.Component;
import java.text.NumberFormat;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;


/**
 * Based on :
 * http://forum.java.sun.com/thread.jspa?threadID=734090&messageID=4220319
 * 
 */
public class PrecisionCellRenderer extends DefaultTableCellRenderer {

    private NumberFormat format;


    public PrecisionCellRenderer(int precision) {
        format = NumberFormat.getNumberInstance();
        format.setMaximumFractionDigits(precision);
        format.setMinimumFractionDigits(0);
    }


    public Component getTableCellRendererComponent(JTable table,
                                                   Object value,
                                                   boolean isSelected,
                                                   boolean hasFocus,
                                                   int row,
                                                   int column) {
        super.getTableCellRendererComponent(table,
                                            value,
                                            isSelected,
                                            hasFocus,
                                            row,
                                            column);
        if (value != null)
            setText(format.format(value));
        return this;
    }
}
